package BlockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: JUC
 * @package: BlockingQueue
 * @className: BlockingQueueDemo
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/3/2 18:49
 * @version: 1.0
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }

    /** 会抛出异常的
     * java.lang.IllegalStateException: Queue full 会抛出队列已经满了的异常
     * java.util.NoSuchElementException  过多移除异常
     * */
    public static void test1() {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println("===============多过加入================");
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        //查看队首元素是谁
        System.out.println(blockingQueue.element());
        ////    此时的队列长度为 3 如果我们此时加入 第四个会怎么样，抛出队列已经满了的异常
        //System.out.println(blockingQueue.add("d"));
        System.out.println("===============过多移除================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
    }

    /*
     * 不报错 返回 false
     * */
    public static void test2() {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println("===============多过加入================");
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        //返回false
        System.out.println(blockingQueue.offer("d"));
        System.out.println(blockingQueue.peek());
        System.out.println("===============过多移除================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //返回null
        System.out.println(blockingQueue.poll());
    }

    /* 一直等待 阻塞
     * */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //blockingQueue.put 队列没有位置了 一支阻塞
        //blockingQueue.put("d");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        //m没有这个元素一直等待
        System.out.println(blockingQueue.take());
    }

    /*等待
    等待超时*/
    public static void test4() throws InterruptedException {
        //队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        //等待，如果设置时间还没有空位置。否则结束
        blockingQueue.offer("d", 2, TimeUnit.SECONDS);
        System.out.println("======================");
        blockingQueue.poll();
        blockingQueue.poll();
        blockingQueue.poll();
        //等待，如果设置时间还没有找到。否则结束
        blockingQueue.poll(2, TimeUnit.SECONDS);

    }
}
